package com.zslin.business.service;

import com.zslin.business.app.dto.orders.OrdersProductDto;
import com.zslin.business.dao.IOrdersProductDao;
import com.zslin.business.dto.OrdersCountSpecsDto;
import com.zslin.business.dto.OrdersCountTotalDto;
import com.zslin.business.model.OrdersProduct;
import com.zslin.core.annotations.AdminAuth;
import com.zslin.core.common.NormalTools;
import com.zslin.core.dto.JsonResult;
import com.zslin.core.tools.JsonTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@AdminAuth(name = "销售统计", psn = "销售管理", orderNum = 2, type = "1", url = "/admin/ordersCount")
public class OrdersCountService {

    @Autowired
    private IOrdersProductDao ordersProductDao;

    public JsonResult index(String params) {
        JsonResult result = JsonResult.getInstance();
        List<OrdersCountSpecsDto> dtoList = new ArrayList<>();
        //格式如:yyyy-MM  或 yyyy-MM-dd
        String day = JsonTools.getJsonParam(params, "date");
        if(day==null || "".equals(day)) { day = NormalTools.curDate(); }
        if(day.length()==7) { //按月
            dtoList = ordersProductDao.findDtoByMonth(day);
        } else if(day.length()==10) { //按日
            dtoList = ordersProductDao.findDtoByDay(day);
        }

        Long totalAmount = 0l; Double totalBackMoney = 0d;
        Double totalMoney = 0d; Double totalRealMoney = 0d;
        Double totalCommissionMoney = 0d;
        for(OrdersCountSpecsDto dto : dtoList) {
            totalAmount += dto.getAmount();
            totalBackMoney += dto.getBackMoney();
            totalMoney += dto.getTotalMoney();
            totalRealMoney += dto.getRealMoney();
            totalCommissionMoney += dto.getCommissionMoney();
        }
        OrdersCountTotalDto dto = new OrdersCountTotalDto(totalAmount, totalBackMoney, totalMoney, totalRealMoney, totalCommissionMoney);
        result.set("totalDto", dto);
        result.set("dtoList", dtoList);
        return result;
    }

    /** 获取详情 */
    public JsonResult listDetail(String params) {
        JsonResult result = JsonResult.getInstance();
        List<OrdersProduct> list = new ArrayList<>();
        String day = JsonTools.getJsonParam(params, "date");
        Integer specsId = JsonTools.getParamInteger(params, "specsId");
        if(day==null || "".equals(day)) { day = NormalTools.curDate(); }
        if(day.length()==7) { //按月
            list = ordersProductDao.findByMonth(day, specsId);
        } else if(day.length()==10) { //按日
            list = ordersProductDao.findByDay(day, specsId);
        }
        result.set("list", list);
        return result;
    }
}
